package com.chen.utils.controller;

import cn.hutool.core.util.StrUtil;
import cn.hutool.extra.servlet.ServletUtil;
import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.serializer.SerializerFeature;
import com.chen.root.base.R;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.SneakyThrows;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import org.w3c.dom.Document;
import org.xml.sax.InputSource;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.xml.parsers.DocumentBuilderFactory;
import javax.xml.transform.OutputKeys;
import javax.xml.transform.Transformer;
import javax.xml.transform.TransformerFactory;
import javax.xml.transform.dom.DOMSource;
import javax.xml.transform.stream.StreamResult;
import java.io.StringReader;
import java.io.StringWriter;
import java.io.Writer;

/**
 * 开发工具接口
 * @Author: ChenJinLong
 * @DateTime: 2023/10/7 11:11
 **/
@Slf4j
@RestController
@RequestMapping("/utils")
@Api(tags = "简单开发工具管理API")
public class UtilsController {

    @Resource
    private HttpServletRequest request;

    @PostMapping("/json")
    @ApiOperation("JSON格式化并转字符串")
    public R<String> jsonFormat(@RequestBody Object json){
        try {
            log.info("客户端IP: {}", ServletUtil.getClientIP(request));
            log.info("JSON格式化: {}", json);
            String jsonStr = JSON.toJSONString(json, SerializerFeature.PrettyFormat,
                    SerializerFeature.WriteMapNullValue, SerializerFeature.WriteDateUseDateFormat);
            return R.ok(jsonStr);
        }catch (Exception e){
            log.error("", e);
            return R.failed(null, "json格式不正确！");
        }
    }

    @PostMapping("/xmlStr")
    @SneakyThrows
    @ApiOperation("XML格式化并转字符串")
    public R<String> xmlStrFormat(@RequestBody String xmlStr){
        if (StrUtil.isBlank(xmlStr)){
            return R.ok("");
        }
        log.info("客户端IP: {}", ServletUtil.getClientIP(request));

        xmlStr = xmlStr.replaceAll("\\\\", "");
        InputSource src = new InputSource(new StringReader(xmlStr));
        Document document = DocumentBuilderFactory.newInstance().newDocumentBuilder().parse(src);

        TransformerFactory transformerFactory = TransformerFactory.newInstance();
        transformerFactory.setAttribute("indent-number", 2);
        Transformer transformer = transformerFactory.newTransformer();
        transformer.setOutputProperty(OutputKeys.ENCODING, "UTF-8");
        transformer.setOutputProperty(OutputKeys.OMIT_XML_DECLARATION, "no");
        transformer.setOutputProperty(OutputKeys.INDENT, "yes");

        Writer out = new StringWriter();
        transformer.transform(new DOMSource(document), new StreamResult(out));
        return R.ok(out.toString());
    }
}
